package com.wanda.controller;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.JsonModelGeneral;
import com.wanda.model.MenuModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.MenuService;
import com.wanda.util.DateFormat;


@Controller
@RequestMapping(value="/qt/menu")
public class MenuController {
	
	@Resource
	private MenuService menuService;
	
	//添加菜单
		@RequestMapping(value="/addMenu.do")
	    public @ResponseBody Map<String,Object> add(MenuModel menuModel , HttpServletRequest request){
			Wd_UserModel user= (Wd_UserModel) request.getSession().getAttribute("bean");
			menuModel.setDelete_time(DateFormat.getNowDate());
			menuModel.setUpdate_time(DateFormat.getNowDate());
			menuModel.setUpdate_user(user.getName());
			menuModel.setDelete_user(user.getName());
			menuService.addMenu(menuModel);
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	System.out.println("添加成功"); 
	    	map.put("msg","成功");
	    	return map;
	    }
		
		@RequestMapping(value="/selectAll.do")
	    public @ResponseBody Object select(){
			System.out.println("+++++++++++++++++++++++++++++++++++++++");
			JsonModelGeneral json = new JsonModelGeneral();
			
			//   menuService.selectAll();
			List<MenuModel> menus =null;
			Map<String,Object> map = new HashMap<String,Object>();
	    	String zNodes="";
			try {
				menus=menuService.selectAll();	 
				json.setRows(menus);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
//			 for(MenuModel menu:menus){	 
//				 System.out.println("ppppp"+menu.getPid());
//	            if(menu.getPid()==null || menu.getId().equals("")){
//	            	System.out.println("父节点");
//	            	 zNodes =zNodes+"{id:"+menu.getId()+",pId:"+menu.getPid()+",name:'"+menu.getMenu_name()+"'},";		 
//	            }else{
//	            	System.out.println("子节点");
//	            	 zNodes =zNodes+"{id:"+menu.getId()+",pId:"+menu.getPid()+",name:'"+menu.getMenu_name()+"',file:'"+menu.getPid()+"'},";
//	            }
//			  }	 
			 System.out.println("menu"+zNodes);
			 
			 map.put("msg", zNodes);
	    	
	    	//map.put("msg","成功");
	    	return json;
	    }
		
		
		@RequestMapping(value="/deleteMenu.do")
	    public @ResponseBody Map<String,Object> deletemenu(MenuModel mod){
		    mod.setDelete_state("1");
			menuService.deleteMenu(mod);
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	System.out.println("删除成功"); 
	    	map.put("msg","成功");
	    	return map;
	    }
	
		@RequestMapping(value="/selectById.do")
	    public @ResponseBody JsonModelGeneral selectById(String menu_id){
		     JsonModelGeneral json = new JsonModelGeneral();
			//menuService.selectById(customer_id);
			json.setRows(menuService.selectById(menu_id));
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	map.put("msg","成功");
	    	return json;
	    }
		
		
		@RequestMapping(value="/updateMenu.do")
	    public @ResponseBody Map<String,Object> updateMenu(MenuModel Menu , HttpServletRequest request){
		
			Wd_UserModel user= (Wd_UserModel) request.getSession().getAttribute("bean");
			Menu.setDelete_time(DateFormat.getNowDate());
			Menu.setUpdate_time(DateFormat.getNowDate());
			Menu.setUpdate_user(user.getName());
			Menu.setDelete_user(user.getName());
			menuService.updateMenu(Menu);		
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	map.put("msg","成功");
	    	return map;
	    }
		
		@RequestMapping(value="/selectByPid.do")
	    public @ResponseBody JsonModelGeneral selectBypid(String pid){
		     JsonModelGeneral json = new JsonModelGeneral();
			//menuService.selectById(customer_id);
		     List<MenuModel> menus =null;
		     List<MenuModel> menus1 =new ArrayList<MenuModel>();
		     menus=menuService.selectAll();	
		     if(pid!=null){
		     for(MenuModel menu:menus){
		    	 if(pid.equals(menu.getPid())){
		    		 menus1.add(menu);
		    	 }
		    	 
		     }
		     }else{
		    	System.err.println("pid=null 报错了");
		     }
			json.setRows(menus1);
			Map<String,Object> map = new HashMap<String,Object>();  
	    	map.put("msg","成功");
	    	return json;
	    }
	
}
